<?php 
class Uploader {

        /**
         * Constructor
         */
        public function __construct(){
        	//current file's directory 
        	$this->_dir = dirname (__FILE__);        	
        	//allow all
        	$this->_allowExtensions=array('*'); 
        	//unlimited
        	$this->_maxSize= -1;   	
        	//msg
        	$this->_msg=null;
        	//auto create destination dir
        	$this->_autoDir=true;
        }
		
        private $_dir;
        private $_allowExtensions=array();
        private $_maxSize;
        private $_msg;      
        private $_autoDir;
		private $_selected_html_filename;
        
        /********************* FUNCTIONS **********************/
        public function upload($file, $path){        	
        	$uploadError = isset($file['error'])?$file['error']:0;
        	$ext=null;
        	$size=0;
			
			ini_set('post_max_size', 100);
			ini_set('max_execution_time', 300);
			ini_set('max_input_time', 300);
			ini_set('upload_max_filesize', 100);
     	
        	if(!is_uploaded_file($file['tmp_name'])){
        		$uploadError=1;
        	}        	
        	
        	if(isset($file['name'])){
				$ext = $this->getFileExtension($file['name']);
			}			
			if(isset($file['size'])){
				$size = $file['size'];
			}else{
				$size = $this->getFileSize($file['tmp_name']);
			}
			
			//upload process
			if($uploadError!=0){
				$this->_msg = 'File upload error!';
				return false;
			}
			
        	if($this->getAutoDir()&& !is_dir($this->getDir())&& !$this->createDir($this->getDir())){
        		$this->_msg = 'Failed to create destination directory!';
        		return false;	
        	}
			
			if($this->getAllowExtensions()!='all' && !in_array($ext,$this->getAllowExtensions())){
				$this->_msg = 'Invalid file extension!';
				return false;
			}

			switch ($path)
			{
			case 'firmware_path':
				$path = '/';
				break;
			case 'html_path':
				$path = '/media/sd/htmls/';
				break;
			case'movie_path':
				$path = '/media/sd/movies/';
				break;
			case 'image_path':
				$image_filename = $this->_selected_html_filename . '_image/';
				$path = '/media/sd/htmls/'.$image_filename;
				break;
			default:
				echo '[NG] [Return] Should Not be here <br>';
				return;
			}
				
	       	if(!move_uploaded_file($file['tmp_name'], $path . $file['name'])) {
	       		$this->_msg = 'Unable to move your uploaded file!';
	       		return false;
	       	}else{
	       		//echo $this->getDir().$file['name'];
	       		//chmod($this->getDir().$file['name'], 0777);
	       		return true;
	       	}
        }

		public function setSelectedHtml($in_strFileName)
		{
			$fileinfo = pathinfo($in_strFileName) ;
			$this->_selected_html_filename = $fileinfo['filename'];
		}

        public function getMsg(){
        	return $this->_msg;
        }
        
        public function setAllowExtensions($ext){
        	if(is_array($ext)){
        		$this->_allowExtensions = array_merge($this->_allowExtensions,$ext);
        	}else{
        		$this->_allowExtensions[]=$ext;
        	}
        }
        
        private function getAllowExtensions(){
        	if(sizeof($this->_allowExtensions)==1 && $this->_allowExtensions[0]=='*'){
        		return 'all';
        	}
        	return $this->_allowExtensions;
        }
		
        public function setDir($dir){
        	$this->_dir = $dir;
        }
        
        public function getDir(){        	
    		$strEnd = substr($this->_dir, strlen($this->_dir)-1);
    		if($strEnd != '/'){
    			$this->_dir.='/';
    		}    		
    		return $this->_dir;
        }

		public function getFileExtension($fileName){
			$pos = strrpos($fileName,'.');
	        if(!$pos){
	            return false;
	        }
	        $str = substr($fileName, $pos, strlen($fileName));
	        return $str;
		}
		
		public function getFileSize($file){
			return filesize($file);
		}

		public function setAutoDir($bool){
			if(is_bool($bool)){
				$this->_autoDir = $bool;
			}
		}
		
		public function getAutoDir(){
			return $this->_autoDir;
		}
		
		public function createDir($path){
			return mkdir($path,0777);
		}
}

?>
